package com.ubeecube.mylocker.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;

import com.ubeecube.mylocker.R;

public class NumberAdapter extends BaseAdapter {

	public static final int MAX_ARRAY_VALUE = 1000;
    public static final int MAX_GALLERY_VALUE = 200;
    public static final int GALLERY_CENTER_VALUE = MAX_GALLERY_VALUE / 2;
	private String[] numberList;
	private Context mContext;
	private TextView mTvNumber;

	/**
     * Class constructor
     * 
     * @param context : Context of activity
     */
    public NumberAdapter(Context context) {
    	this.mContext = context;
        this.numberList = context.getResources().getStringArray(R.array.numbers);;
    }
    
    /**
     * Get item position
     * 
     * @param position : item position
     * @return position
     */
    public int getPosition(int position) {

//        if (position >= GALLERY_CENTER_VALUE) {
//            position = position - GALLERY_CENTER_VALUE;
//        } else {
//            position = GALLERY_CENTER_VALUE - position;
//        }

        if (numberList != null && numberList.length > 0) {
            position = position % numberList.length;
        }
        
        return position;
    }
	
	@Override
	public int getCount() {
		return MAX_GALLERY_VALUE;
	}

	@Override
	public Object getItem(int position) {
		return numberList[getPosition(position)];
	}

	@Override
	public long getItemId(int position) {
		 return getPosition(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.number_row_layout, parent, false);
//        Log.e("getView", " before position : " + position); 
        position = getPosition(position);
//        Log.e("getView", " after position : " + position);
        
        convertView.setLayoutParams(new Gallery.LayoutParams(200, 60)); 
        mTvNumber = (TextView) convertView.findViewById(R.id.locker_number);
        mTvNumber.setText(numberList[position]);
        
        
		return convertView;
	}

}
